package com.xxxx.server.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

// Redis配置类
//
@Api("redis配置类")
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> template = new RedisTemplate();

        // String类型，key序列器
        template.setKeySerializer(new StringRedisSerializer());
        // String类型，key序列器
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // Hash类型，key序列器
        template.setHashKeySerializer(new StringRedisSerializer());
        // Hash类型，value序列器
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
